package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("selectAll")
    @ResponseBody
    @LogAnnotation(content="查询全部用户",type="查询")
    public Map selectAll(Integer page,Integer limit,String userTelphone){
        Map map = new HashMap();
        PageInfo<User> pageInfo = userService.selectAll(page, limit, userTelphone);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("updateStatus")
    @ResponseBody
    @LogAnnotation(content="更新用户状态",type="更新")
    public Map updateStatus(Integer userStatus,Integer userId){
        Map map = new HashMap();
        try {
            userService.updateStatus(userStatus,userId);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("selectNum")
    @ResponseBody
    @LogAnnotation(content="查询男女人数",type="查询")
    public List<Integer> selectNum(){
        List<Integer> list = userService.selectNum();
        return list;
    }
    @RequestMapping("selectProvince")
    @ResponseBody
    @LogAnnotation(content="查询省份信息",type="查询")
    public List<Map> selectProvince(){
        List<Map> maps = userService.selectByProvince();
        return maps;
    }
    @LogAnnotation(content="查询注册人数",type="查询")
    @RequestMapping("selectWeek")
    @ResponseBody
    public List<Integer> selectWeek(){
        List<Integer> list = userService.selectWeek();
        return list;
    }
    @RequestMapping("selectAllCountThread")
    @ResponseBody
    @LogAnnotation(content="查询优化数据",type="查询")
    public Map selectAllCountThread() throws Exception {
        long start = System.currentTimeMillis();
        Map map = userService.selectByThread();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }
    @LogAnnotation(content="查询无优化数据",type="查询")
    @RequestMapping("selectAllCount")
    @ResponseBody
    public Map selectAllCount() {
        long start = System.currentTimeMillis();
        Map map = userService.selectAllCount();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }
}
